package com.global.college.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.constants.Constant;
import com.global.college.ui.bean.UploadHeaderModel;
import com.global.college.ui.bean.UserDataModel;
import com.global.college.ui.interfaces.IUserInfoView;
import com.global.college.ui.presenter.UserInfoPresenter;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 编辑昵称等信息
 */
public class EditUserInfoActivity extends BaseActivity implements IUserInfoView {

    @BindView(R.id.et_content)
    ClearEditText etContent;
    @BindView(R.id.tv_save)
    TextView tvSave;
    String strNickname,strNicknameOrigin;
    private UserInfoPresenter userInfoPresenter;
    @Override
    public int getContentViewId() {
        return R.layout.activity_edit_user_info;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(true);
        setShowBack(true);
        setTitle("修改昵称");
        userInfoPresenter = new UserInfoPresenter(this);
        userInfoPresenter.setUserInfoViewView(this);
        strNicknameOrigin = getIntent().getStringExtra(Constant.Key.NAME);
        if (StringUtils.isNotEmpty(strNicknameOrigin,true)){
            etContent.setText(strNicknameOrigin);
        }

    }

    @Override
    public void getInitData() {
        initListener();
        tvSave.setEnabled(false);
    }

    private void initListener() {
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strNickname = charSequence.toString();

                if (StringUtils.isNotEmpty(strNickname, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({ R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                submitData();
                break;
        }
    }

    private void submitData() {
        strNickname = etContent.getText().toString();
        if (StringUtils.isEmpty(strNickname)){
            ToastUtils.showCenterToast("请输入昵称");
            return;
        }
        if (StringUtils.equals(strNicknameOrigin,strNickname)){
            ToastUtils.showCenterToast("昵称未做修改");
            return;
        }
        userInfoPresenter.setUserInfo("",strNickname,"", UserData.getInstance().getPhone());
    }

    @Override
    public void onGetUserInfoSuccess(UserDataModel registModel) {

    }

    @Override
    public void onGetUserInfoFail(String error) {

    }

    @Override
    public void onSetUserInfoSuccess(Object smsModel) {
        ToastUtils.showCenterToast("保存成功！");
        finish();
    }

    @Override
    public void onSetUserInfoFail(String error) {

    }

    @Override
    public void onUploadHeaderSuccess(UploadHeaderModel smsModel) {

    }

    @Override
    public void onUploadHeaderFail(String error) {

    }
}